﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.IO;
using System.Runtime.Serialization;
using Oracle.DataAccess.Client;
using System.Data;
using LCUV;

namespace sWarranty.WEB
{
    /// <summary>
    /// Summary description for Handler1
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.ContentType = "application/json";
            context.Response.Charset = "charset=utf-8";

            string md = HttpContext.Current.Request.QueryString["v"];
            JavaScriptSerializer ser = new JavaScriptSerializer();
            List<SelectOptions> model = new List<SelectOptions>();

            try
            {
                DataTable dt = new DataTable();
                dt = DBAction.runStore("PK_SOM_MST.PR_PROD_MODEL_TRC", "uiroi", "p_model", md,
                   null, null, null, null,
                    null, null, null, null, null, null, null, null, null, null, null, null
                    , null, null, null, null, null, null, null, null, null, null, null, null);
                try
                {
                    if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                    {
                        model.Add(new SelectOptions { text = dt.Rows[0]["Error"].ToString(), id = "0" });
                        context.Response.Write(ser.Serialize(model));
                    }
                }
                catch (Exception)
                {
                    //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
                }
                int i = 1;

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        model.Add(new SelectOptions
                        {
                            text ="PRD_MODEL: " +  item["PROD_MODEL_SUFFIX"].ToString() + "\nSAL_MODEL:" + item["SALES_MODEL"].ToString()
                                + "." + item["SALES_SUFFIX"].ToString(),
                            id = i++.ToString()
                        });
                        i++;
                    }
                    context.Response.Write(ser.Serialize(model));
                }
                else
                {
                    model.Add(new SelectOptions { text = "No result of: " + md, id = "0" });
                    context.Response.Write(ser.Serialize(model));
                }
            }
            catch (Exception ee)
            {
                //return ee.Message;
                model.Add(new SelectOptions { text = ee.Message, id = "0" });
                context.Response.Write(ser.Serialize(model));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        [DataContract]
        public class SelectOptions
        {
            [DataMember]
            public string id { get; set; }
            [DataMember]
            public string text { get; set; }

        }
    }
}